package com.huilam.restactions.common;

import java.util.Enumeration;
import java.util.Properties;
import java.util.Vector;

import com.huilam.restactions.common.util.ClassLoaderUtil;

public class RESTAction {
	
	private String moduleId 	= null;
	private String modulePath 	= null;
	private String className 	= null;
	private boolean debugMode	= true;
	private Properties propConfig	= null;
	
	private Vector<String> requestMethods	= null;
	
	
	private ClassLoader classLoader = getClass().getClassLoader();
	
	public RESTAction(String aModuleId, String aModulePath, Vector<String> aRequestMethods)
	{
		requestMethods = new Vector<String>();
		setModuleId(aModuleId);
		setModulePath(aModulePath);
		propConfig = new Properties();
		requestMethods = aRequestMethods;
	}
	
	public void setAppResources(String aAppResources)
	{
		if(aAppResources!=null && aAppResources.length()>0)
		{
			classLoader = ClassLoaderUtil.getClassLoader(
					aAppResources, 
					getClass().getClassLoader());
		}
		else
		{
			classLoader = getClass().getClassLoader();
		}
	}
	
	public Class getRESTActionClass() throws ClassNotFoundException
	{
		return classLoader.loadClass(getClassName());
	}
	
	public void addConfigProp(String aKey, String aValue)
	{
		propConfig.put(aKey, aValue);
	}
	
	public String getConfigProp(String aKey)
	{
		return (String) propConfig.get(aKey);
	}
		
	public String getModulePath() {
		return modulePath;
	}

	public void setModulePath(String modulePath) {
		this.modulePath = modulePath;
	}
	
	public String getModuleId() {
		return moduleId;
	}

	public void setModuleId(String moduleId) {
		this.moduleId = moduleId;
	}

	public Vector<String> getRequestMethods() {
		return requestMethods;
	}

	public void setRequestMethods(Vector<String> requestMethods) {
		this.requestMethods = requestMethods;
	}

	public boolean isDebugMode() {
		return debugMode;
	}
	public void setDebugMode(boolean DebugMode) {
		this.debugMode = DebugMode;
	}

	public String getClassName() {
		return className;
	}
	public void setClassName(String actionClassName) {
		this.className = actionClassName;
	}
	
	public String toString()
	{
		StringBuffer sb = new StringBuffer();
		sb.append("\n").append("moduleId=").append(getModuleId());
		sb.append("\n").append("modulePath=").append(getModulePath());
		sb.append("\n").append("className=").append(getClassName());
		sb.append("\n").append("debugMode=").append(isDebugMode());
		sb.append("\n").append("requestMethods=").append(getRequestMethods());
		
		Enumeration e = propConfig.keys();
		while(e.hasMoreElements())
		{
			String sKey = (String) e.nextElement();
			sb.append("\npropConfig@").append(sKey).append("=").append(propConfig.get(sKey));
		}
		return sb.toString();
	}
	
}
